package com.example.moshudriver.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.BeeFramework.model.BusinessResponse;
import com.BeeFramework.view.ToastView;
import com.sgr.dykdriver.R;
import com.example.moshudriver.model.UserModel;
import com.example.moshudriver.protocol.ApiInterface;
import com.example.moshudriver.tools.O2OMobileAppConst;
import com.example.moshudriver.utils.MyApplication;
import com.external.androidquery.callback.AjaxStatus;
import com.zxing.activity.CaptureActivity;

public class B1_SignupLeadActivity extends Activity implements OnClickListener,
		BusinessResponse {

	private Button hasInviteCode;
	private Button noInviteCode;
	private SharedPreferences mShared;
	private Editor mEditor;
	private EditText b1_edit_initv;
	private UserModel mUserModel;
	private Button btn_scan_qr_code;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.b1_signup_lead);
		mShared = getSharedPreferences(O2OMobileAppConst.USERINFO, 0);
		mEditor = mShared.edit();
		initView();
		mUserModel = new UserModel(this);
		mUserModel.addResponseListener(this);
		MyApplication.getInstance().addActivity(this);

	}

	private void initView() {
		hasInviteCode = (Button) findViewById(R.id.btn_has_invite_code);
		noInviteCode = (Button) findViewById(R.id.btn_no_invite_code);
		b1_edit_initv = (EditText) findViewById(R.id.b1_edit_initv);
		btn_scan_qr_code = (Button) findViewById(R.id.btn_scan_qr_code);
		btn_scan_qr_code.setOnClickListener(this);
		hasInviteCode.setOnClickListener(this);
		noInviteCode.setOnClickListener(this);
		CloseKeyBoard();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		// 有邀请码
		case R.id.btn_has_invite_code:
			String invite = b1_edit_initv.getText().toString();

			if (invite != null && invite.length() == 6) {
				mUserModel.checkInviteCode(invite);
			} else {
				ToastView toast = new ToastView(this,
						getString(R.string.invite_wrong_format_hint));
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
			}

			break;
		// 无邀请码
		case R.id.btn_no_invite_code:

			Intent intent2 = new Intent(this, B1_SignupVerifyActivity.class);
			intent2.putExtra(B1_SignupVerifyActivity.INVITE, "");
			startActivity(intent2);
			finish();
			break;

		// 扫描二维码
		case R.id.btn_scan_qr_code:
			// 打开扫描界面扫描条形码或二维码
			Intent openCameraIntent = new Intent(B1_SignupLeadActivity.this,
					CaptureActivity.class);
			startActivityForResult(openCameraIntent, 0);
			break;
		default:
			break;
		}

	}

	private void CloseKeyBoard() {
		// b1_edit_initv.requestFocus();
		InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(b1_edit_initv.getWindowToken(), 0);
	}

	@Override
	public void OnMessageResponse(String url, JSONObject jo, AjaxStatus status)
			throws JSONException {
		if (url.endsWith(ApiInterface.USER_INVITE_CHECK)) {
			Intent intent1 = new Intent(this, B1_SignupVerifyActivity.class);
			intent1.putExtra(B1_SignupVerifyActivity.INVITE, b1_edit_initv
					.getText().toString());
			
			startActivity(intent1);
			finish();
		}

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		// 处理扫描结果（在界面上显示）
		if (resultCode == RESULT_OK) {
			Bundle bundle = data.getExtras();
			String scanResult = bundle.getString("result");
			b1_edit_initv.setText(scanResult);
		}
	}
}
